//
//  UIButton+Lc.m
//  18Birdies
//
//  Created by Lc on 15/3/31.
//  Copyright (c) 2015年 Lc. All rights reserved.
//

#import <objc/runtime.h>
#import "LcFont.h"
#import "LcPreconditions.h"
#import "NSAttributedString+Lc.h"
#import "UIButton+Lc.h"
#import "UIColor+Lc.h"
#import "UIImage+Lc.h"
#import "UILabel+Lc.h"
#import "LcUIUtility.h"
#import "UIDevice+Lc.h"

@implementation UIButton (Lc)

+ (UIButton*)commonDarkNavigationButtonWithTitle:(NSString*)title
                                          target:(id)target
                                          action:(SEL)action {
  LC_CHECK_NOT_EMPTY(title);
  LC_CHECK_NOT_NULL(target);
  UIButton* button = [self buttonWithType:UIButtonTypeCustom];
  [button setTitle:title forState:UIControlStateNormal];
  [button setLcFont:[LcFont dm17]];
  [button setTitleColor:LC_COLOR_DARK_ACTIVE forState:UIControlStateHighlighted];
  [button setCommonNavigationStyle];
  [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
  return button;
}

+ (UIButton*)commonWhiteNavigationButtonWithTitle:(NSString*)title
                                           target:(id)target
                                           action:(SEL)action {
  LC_CHECK_NOT_EMPTY(title);
  LC_CHECK_NOT_NULL(target);
  UIButton* button = [self buttonWithType:UIButtonTypeCustom];
  [button setTitle:title forState:UIControlStateNormal];
  [button setLcFont:[LcFont wm17]];
  [button setTitleColor:LC_COLOR_WHITE_80 forState:UIControlStateHighlighted];
  [button setTitleColor:LC_COLOR_WHITE_60 forState:UIControlStateDisabled];
  [button setCommonNavigationStyle];
  [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
  return button;
}

+ (UIButton*)commonWhiteNavigationButtonWithTitle:(NSString*)title
                                           target:(id)target
                                           action:(SEL)action font:(LcFont*)font {
  UIButton* button = [UIButton commonWhiteNavigationButtonWithTitle:title target:target action:action];
  [button setLcFont:font];
  return button;
}

+ (UIButton*)commonBlueNavigationButtonWithTitle:(NSString*)title
                                          target:(id)target
                                          action:(SEL)action {
  LC_CHECK_NOT_EMPTY(title);
  LC_CHECK_NOT_NULL(target);
  UIButton* button = [self buttonWithType:UIButtonTypeCustom];
  [button setTitle:title forState:UIControlStateNormal];
  [button setLcFont:[LcFont bb17]];
  [button setTitleColor:LC_COLOR_BLUE_ACTIVE forState:UIControlStateHighlighted];
  [button setCommonNavigationStyle];
  [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
  return button;
}

+ (UIButton*)commonBlueBackButtonWithTitle:(NSString*)title target:(id)target action:(SEL)action {
  LC_CHECK_NOT_EMPTY(title);
  LC_CHECK_NOT_NULL(target);
  UIButton* button = [self buttonWithType:UIButtonTypeCustom];
  [button setCommonBackStyle];
  [button setTitle:title forState:UIControlStateNormal];
  [button setLcFont:[LcFont bm17]];
  [button setTitleColor:LC_COLOR_BLUE_ACTIVE forState:UIControlStateHighlighted];
  [button setImage:[UIImage imageNamed:@"common_button_back_normal"] forState:UIControlStateNormal];
  [button setImage:[UIImage imageNamed:@"common_button_back_active"]
          forState:UIControlStateHighlighted];
  [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
  return button;
}

+ (UIButton*)commonDarkBackButtonWithTitle:(NSString*)title target:(id)target action:(SEL)action {
  LC_CHECK_NOT_EMPTY(title);
  LC_CHECK_NOT_NULL(target);
  UIButton* button = [self buttonWithType:UIButtonTypeCustom];
  [button setCommonBackStyle];
  [button setTitle:title forState:UIControlStateNormal];
  [button setLcFont:[LcFont dm17]];
  [button setTitleColor:LC_COLOR_BLUE_ACTIVE forState:UIControlStateHighlighted];
  [button setImage:[UIImage imageNamed:@"navbar_back"]
          forState:UIControlStateNormal];
  [button setImage:[UIImage imageNamed:@"navbar_back"]
          forState:UIControlStateHighlighted];
  [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
  return button;
}

+ (UIButton*)commonWhiteBackButtonWithTitle:(NSString*)title target:(id)target action:(SEL)action {
  LC_CHECK_NOT_EMPTY(title);
  LC_CHECK_NOT_NULL(target);
  UIButton* button = [self buttonWithType:UIButtonTypeCustom];
  [button setCommonBackStyle];
  [button setTitle:title forState:UIControlStateNormal];
  [button setLcFont:[LcFont wm17]];
  [button setTitleColor:LC_COLOR_WHITE_80 forState:UIControlStateHighlighted];
  [button setImage:[UIImage imageNamed:@"common_button_back_white_normal"]
          forState:UIControlStateNormal];
  [button setImage:[UIImage imageNamed:@"common_button_back_white_active"]
          forState:UIControlStateHighlighted];
  [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
  return button;
}

+ (UIButton*)closeButtonWithTarget:(id)target action:(SEL)action {
  LC_CHECK_NOT_NULL(target);
  const CGFloat WIDTH = 20;
  const CGFloat HEIGHT = 20;
  UIButton* button = [self buttonWithType:UIButtonTypeCustom];
  [button setBackgroundImage:[UIImage imageNamed:@"pop_close_normal"]
                    forState:UIControlStateNormal];
  [button setBackgroundImage:[UIImage imageNamed:@"pop_close_active"]
                    forState:UIControlStateHighlighted];
  button.frame = CGRectMake(0, 0, WIDTH, HEIGHT);
  [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
  return button;
}

+ (UIButton*)whiteCloseButtonWithTarget:(id)target action:(SEL)action {
  LC_CHECK_NOT_NULL(target);
  const CGFloat WIDTH = 20;
  const CGFloat HEIGHT = 20;
  UIButton* button = [self buttonWithType:UIButtonTypeCustom];
  [button setBackgroundImage:[UIImage imageNamed:@"toast_close_normal"]
                    forState:UIControlStateNormal];
  [button setBackgroundImage:[UIImage imageNamed:@"toast_close_active"]
                    forState:UIControlStateHighlighted];
  button.frame = CGRectMake(0, 0, WIDTH, HEIGHT);
  [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
  return button;
}

+ (UIButton*)refreshButtonWithTarget:(id)target action:(SEL)action {
  LC_CHECK_NOT_NULL(target);
  const CGFloat WIDTH = 29;
  const CGFloat HEIGHT = 29;
  UIButton* button = [self buttonWithType:UIButtonTypeCustom];
  [button setBackgroundImage:[UIImage imageNamed:@"common_button_refresh_normal"]
                    forState:UIControlStateNormal];
  [button setBackgroundImage:[UIImage imageNamed:@"common_button_refresh_active"]
                    forState:UIControlStateHighlighted];
  button.frame = CGRectMake(0, 0, WIDTH, HEIGHT);
  [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
  return button;
}

+ (UIButton*)moreButtonWithTarget:(id)target action:(SEL)action {
  return [self rightNavigationButtonWithTarget:target
                                        action:action
                               normalImageName:@"common_button_more_normal"
                          highlightedImageName:@"common_button_more_active"];
}

+ (UIButton*)searchButtonWithTarget:(id)target action:(SEL)action {
  LC_CHECK_NOT_NULL(target);
  const CGFloat WIDTH = 29;
  const CGFloat HEIGHT = 29;
  UIButton* button = [self buttonWithType:UIButtonTypeCustom];
  [button setImage:[UIImage imageNamed:@"common_button_search_normal"]
          forState:UIControlStateNormal];
  [button setImage:[UIImage imageNamed:@"common_button_search_active"]
          forState:UIControlStateHighlighted];
  button.frame = CGRectMake(0, 0, WIDTH, HEIGHT);
  [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
  return button;
}

+ (UIButton*)shareButtonWithTarget:(id)target action:(SEL)action {
  LC_CHECK_NOT_NULL(target);
  return [self rightNavigationButtonWithTarget:target action:action imageName:@"icon_share"];
}

+ (UIButton*)rightNavigationButtonWithTarget:(id)target
                                      action:(SEL)action
                                   imageName:(NSString*)imageName {
  return [self rightNavigationButtonWithTarget:target
                                        action:action
                               normalImageName:imageName
                          highlightedImageName:imageName];
}

+ (UIButton*)rightNavigationButtonWithTarget:(id)target
                                      action:(SEL)action
                             normalImageName:(NSString*)normalImageName
                        highlightedImageName:(NSString*)highlightedImageName {
  LC_CHECK_NOT_NULL(target);
  CGFloat WIDTH = 59;
  if ([UIDevice currentDevice].systemVersion.doubleValue < 11.0 && [UIDevice isSmallUISize]) {
    WIDTH = 44;
  }
  const CGFloat HEIGHT = 30;
  UIButton* button = [self buttonWithType:UIButtonTypeCustom];
  button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
  [button setImage:[UIImage imageNamed:normalImageName] forState:UIControlStateNormal];
  [button setImage:[UIImage imageNamed:highlightedImageName] forState:UIControlStateHighlighted];
  button.frame = CGRectMake(0, 0, WIDTH, HEIGHT);
  [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
  if (@available(iOS 11, *)) {
    button.contentEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 15);
  } else {
    button.contentEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 7);
  }
  return button;
}

+ (UIButton*)customButtonWithImageName:(NSString*)imageName target:(id)target action:(SEL)action {
  LC_CHECK_NOT_NULL(target);
  const CGFloat WIDTH = 30;
  const CGFloat HEIGHT = 30;
  UIButton* button = [self buttonWithType:UIButtonTypeCustom];
  [button setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
  [button setImage:[UIImage imageNamed:imageName] forState:UIControlStateHighlighted];
  button.frame = CGRectMake(0, 0, WIDTH, HEIGHT);
  [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
  return button;
}

+ (UIButton*)customButtonWithTitle:(NSString*)title
                         imageName:(NSString*)imageName
                            target:(id)target
                            action:(SEL)action {
  LC_CHECK_NOT_EMPTY(title);
  LC_CHECK_NOT_EMPTY(imageName);
  LC_CHECK_NOT_NULL(target);
  const CGFloat WIDTH = 70;
  const CGFloat HEIGHT = 30;
  UIButton* button = [self buttonWithType:UIButtonTypeCustom];
  button.frame = CGRectMake(0, 0, WIDTH, HEIGHT);
  [button setContentHorizontalAlignment:UIControlContentHorizontalAlignmentRight];
  [button setTitle:title forState:UIControlStateNormal];
  [button setLcFont:[LcFont wb14]];
  [button setTitleColor:LC_COLOR_WHITE forState:UIControlStateHighlighted];
  [button setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
  [button setImage:[UIImage imageNamed:imageName] forState:UIControlStateHighlighted];
  button.imageEdgeInsets =
      UIEdgeInsetsMake(0, button.frame.size.width - button.imageView.frame.origin.x -
                              button.imageView.frame.size.width + 15,
                       0, 0);
  button.titleEdgeInsets =
      UIEdgeInsetsMake(0, -(button.frame.size.width - button.imageView.frame.size.width), 0,
                       button.imageView.frame.size.width + 15);
  [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
  return button;
}

- (void)setNormalStateTitleWithUnderlineString:(NSString*)string lcFont:(LcFont*)lcFont {
  LC_CHECK_NOT_EMPTY(string);
  LC_CHECK_NOT_NULL(lcFont);
  NSAttributedString* attributedString =
      [NSAttributedString underlineStringWithString:string lcFont:lcFont];
  [self setTitle:nil forState:UIControlStateNormal];
  [self setAttributedTitle:attributedString forState:UIControlStateNormal];
}

- (void)setBlueStyle {
  [self setBlueStyleWithRoundCorner:YES];
}

- (void)setBlueStyleWithRoundCorner:(BOOL)roundCorner {
  [self setLcFont:[LcFont wb17]];
  [self setBackgroundImage:[UIImage imageWithColor:LC_COLOR_BLUE] forState:UIControlStateNormal];
  [self setBackgroundImage:[UIImage imageWithColor:LC_COLOR_BLUE_ACTIVE]
                  forState:UIControlStateHighlighted];
  [self setBackgroundImage:[UIImage imageWithColor:LC_COLOR_SECONDARY_GRAY]
                  forState:UIControlStateDisabled];

  if (roundCorner) {
    self.layer.cornerRadius = 5;
    self.layer.borderColor = nil;
    self.layer.borderWidth = 0.0f;
    self.layer.masksToBounds = YES;
  }
}

- (void)setSmallBlueStyle {
  [self setLcFont:[LcFont wb14]];
  [self setBackgroundImage:[UIImage imageWithColor:LC_COLOR_BLUE] forState:UIControlStateNormal];
  [self setBackgroundImage:[UIImage imageWithColor:LC_COLOR_BLUE_ACTIVE]
                  forState:UIControlStateHighlighted];
  self.layer.cornerRadius = 5;
  self.layer.borderColor = nil;
  self.layer.borderWidth = 0.0f;
  self.layer.masksToBounds = YES;
}

- (void)setBlueBorderedStyle {
  [self setLcFont:[LcFont bb17]];
  self.layer.cornerRadius = 5;
  self.layer.borderWidth = 1;
  self.layer.borderColor = LC_COLOR_BLUE.CGColor;
  self.layer.masksToBounds = YES;
  [self setBackgroundImage:nil forState:UIControlStateNormal];
  [self setBackgroundImage:[UIImage imageWithColor:[LC_COLOR_BLUE colorWithAlphaComponent:0.2]]
                  forState:UIControlStateHighlighted];
}

- (void)setSmallBlueBorderedStyle {
  [self setLcFont:[LcFont bb14]];
  self.layer.cornerRadius = 5;
  self.layer.borderWidth = 1;
  self.layer.borderColor = LC_COLOR_BLUE.CGColor;
  self.layer.masksToBounds = YES;
  [self setBackgroundImage:[UIImage imageWithColor:[LC_COLOR_BLUE colorWithAlphaComponent:0.2]]
                  forState:UIControlStateHighlighted];
}

- (void)setWhiteBorderedStyle {
  [self setLcFont:[LcFont wb17]];
  self.layer.cornerRadius = 5;
  self.layer.borderWidth = 1;
  self.layer.borderColor = LC_COLOR_WHITE.CGColor;
  self.layer.masksToBounds = YES;
  [self setBackgroundImage:[UIImage imageWithColor:[LC_COLOR_WHITE colorWithAlphaComponent:0.2]]
                  forState:UIControlStateHighlighted];
}

- (void)setSmallWhiteBorderedStyle {
  [self setLcFont:[LcFont wb14]];
  self.layer.cornerRadius = 5;
  self.layer.borderWidth = 1;
  self.layer.borderColor = LC_COLOR_WHITE.CGColor;
  self.layer.masksToBounds = YES;
  [self setBackgroundImage:[UIImage imageWithColor:[LC_COLOR_WHITE colorWithAlphaComponent:0.2]]
                  forState:UIControlStateHighlighted];
}

- (void)setRoundWhiteBorderedStyle {
  [self setLcFont:[LcFont wb17]];
  self.layer.cornerRadius = 22;
  self.layer.borderWidth = 1;
  self.layer.borderColor = LC_COLOR_WHITE.CGColor;
  self.layer.masksToBounds = YES;
  [self setBackgroundImage:[UIImage imageWithColor:[LC_COLOR_WHITE colorWithAlphaComponent:0.2]]
                  forState:UIControlStateHighlighted];
  UIEdgeInsets edgeInsets = self.contentEdgeInsets;
  edgeInsets.left = 5 + 15;
  edgeInsets.right = 5 + 15;
  self.contentEdgeInsets = edgeInsets;
}

- (void)setSmallRoundWhiteBorderedStyle {
  [self setLcFont:[LcFont wb14]];
  self.layer.cornerRadius = 14.5;
  self.layer.borderWidth = 1;
  self.layer.borderColor = LC_COLOR_WHITE.CGColor;
  self.layer.masksToBounds = YES;
  [self setBackgroundImage:[UIImage imageWithColor:[LC_COLOR_WHITE colorWithAlphaComponent:0.2]]
                  forState:UIControlStateHighlighted];
  UIEdgeInsets edgeInsets = self.contentEdgeInsets;
  edgeInsets.left = 15;
  edgeInsets.right = 15;
  self.contentEdgeInsets = edgeInsets;
}

- (void)setSmallerRoundWhiteBorderedStyle {
  [self setLcFont:[LcFont wb14]];
  self.layer.cornerRadius = 5;
  self.layer.borderWidth = 2;
  self.layer.borderColor = LC_COLOR_WHITE.CGColor;
  self.layer.masksToBounds = YES;
  [self setBackgroundImage:[UIImage imageWithColor:[LC_COLOR_DARK colorWithAlphaComponent:0.3]]
                  forState:UIControlStateNormal];
  [self setBackgroundImage:[UIImage imageWithColor:[LC_COLOR_WHITE colorWithAlphaComponent:0.2]]
                  forState:UIControlStateHighlighted];
  UIEdgeInsets edgeInsets = self.contentEdgeInsets;
  edgeInsets.left = 15;
  edgeInsets.right = 15;
  self.contentEdgeInsets = edgeInsets;
}

- (void)setGrayBorderedStyle {
  [self setLcFont:[LcFont fontWithColor:LC_COLOR_GRAY
                             fontWeight:LC_FONT_WEIGHT_REGULAR
                            andFontSize:17]];
  self.layer.cornerRadius = 5;
  self.layer.borderWidth = 1;
  self.layer.borderColor = LC_COLOR_GRAY.CGColor;
  self.layer.masksToBounds = YES;
  [self setBackgroundImage:nil forState:UIControlStateNormal];
  [self setBackgroundImage:[UIImage imageWithColor:[LC_COLOR_GRAY colorWithAlphaComponent:0.2]]
                  forState:UIControlStateHighlighted];
}

- (void)setBlueCellStyle {
  [self setLcFont:[LcFont bb17]];

  [self setTitleColor:LC_COLOR_BLUE_ACTIVE forState:UIControlStateHighlighted];
  [self setBackgroundImage:nil forState:UIControlStateNormal];
  [self setBackgroundImage:[UIImage imageWithColor:LC_COLOR_THIRD_GRAY]
                  forState:UIControlStateHighlighted];
}

- (void)setBlueNoBgImageStyle {
  [self setLcFont:[LcFont bb17]];
  [self setTitleColor:LC_COLOR_BLUE_ACTIVE forState:UIControlStateHighlighted];
}

- (void)setWhiteNoBgImageStyle {
  [self setLcFont:[LcFont wr17]];
  [self setTitleColor:LC_COLOR_WHITE_80 forState:UIControlStateHighlighted];
}

- (void)setBlueLinerGradientStyle {
  [self setLcFont:[LcFont wb17]];
  self.layer.cornerRadius = 5;
  self.layer.masksToBounds = YES;
  [self setBackgroundImage:[UIImage linearImageWithStartColor:LC_COLOR_BLUE
                                                     endColor:LC_COLOR_BLUE_OCEAN
                                                      andSize:self.bounds.size]
                  forState:UIControlStateNormal];
  [self setBackgroundImage:[UIImage imageWithColor:LC_COLOR_BLUE_ACTIVE]
                  forState:UIControlStateHighlighted];
}

- (void)setDarkCellStyle {
  [self setLcFont:[LcFont db17]];

  [self setTitleColor:LC_COLOR_DARK forState:UIControlStateHighlighted];
  [self setBackgroundImage:nil forState:UIControlStateNormal];
  [self setBackgroundImage:[UIImage imageWithColor:LC_COLOR_THIRD_GRAY]
                  forState:UIControlStateHighlighted];
}

- (void)setGrayCellStyle {
  [self setLcFont:[LcFont gb17]];

  [self setTitleColor:LC_COLOR_DARK forState:UIControlStateHighlighted];
  [self setBackgroundImage:nil forState:UIControlStateNormal];
  [self setBackgroundImage:[UIImage imageWithColor:LC_COLOR_THIRD_GRAY]
                  forState:UIControlStateHighlighted];
}

- (void)setGrayStyle {
  self.layer.cornerRadius = 5;
  self.layer.masksToBounds = YES;
  [self setLcFont:[LcFont wb17]];
  [self setBackgroundImage:[UIImage imageWithColor:LC_COLOR_GRAY] forState:UIControlStateNormal];
}

- (void)setSecondaryGrayStyle {
  self.layer.cornerRadius = 5;
  self.layer.masksToBounds = YES;
  [self setLcFont:[LcFont wb17]];
  [self setBackgroundImage:[UIImage imageWithColor:LC_COLOR_SECONDARY_GRAY]
                  forState:UIControlStateNormal];
}

- (void)setWhiteBackgroundStyle:(UIColor*)titleColor {
  [self setLcFont:[LcFont fontWithColor:titleColor fontWeight:LC_FONT_WEIGHT_BOLD andFontSize:17]];
  self.layer.cornerRadius = 5;
  self.layer.borderWidth = 1;
  self.layer.borderColor = titleColor.CGColor;
  self.clipsToBounds = YES;
  [self setBackgroundImage:[UIImage imageWithColor:LC_COLOR_WHITE] forState:UIControlStateNormal];
  [self setBackgroundImage:[UIImage imageWithColor:[LC_COLOR_WHITE colorWithAlphaComponent:0.5]]
                  forState:UIControlStateHighlighted];
}

- (void)setWhiteStyle {
  [self setLcFont:[LcFont dm17]];
  self.layer.cornerRadius = 5;
  self.clipsToBounds = YES;
  [self setBackgroundImage:[UIImage imageWithColor:LC_COLOR_WHITE] forState:UIControlStateNormal];
  [self setBackgroundImage:[UIImage imageWithColor:LC_COLOR_SECONDARY_GRAY]
                  forState:UIControlStateHighlighted];
}

- (void)setWhiteStyleWithoutRoundCorner {
  [self setLcFont:[LcFont dm17]];
  [self setBackgroundImage:[UIImage imageWithColor:LC_COLOR_WHITE] forState:UIControlStateNormal];
  [self setBackgroundImage:[UIImage imageWithColor:LC_COLOR_SECONDARY_GRAY]
                  forState:UIControlStateHighlighted];
}

- (void)setDisableStyle {
  [self setLcFont:[LcFont wm14]];
  self.enabled = NO;
  [self setBackgroundImage:[UIImage imageWithColor:LC_COLOR_GRAY] forState:UIControlStateDisabled];
}

- (void)setRedFontStyle {
  [self setLcFont:LcFont.lrb17];
  [self setBackgroundColor:[UIColor whiteColor]];
  [self setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
  [self setBackgroundImage:nil forState:UIControlStateNormal];
  [self setBackgroundImage:[UIImage imageWithColor:LC_COLOR_THIRD_GRAY]
                  forState:UIControlStateHighlighted];
}

- (void)setRedFontBorderedStyle {
  [self setRedFontStyle];
  self.layer.cornerRadius = 5;
  self.layer.masksToBounds = YES;
}

- (void)setOrangeCellStyle {
  [self setLcFont:[LcFont ob17]];
  [self setTitleColor:LC_COLOR_ORANGE_ACTIVE forState:UIControlStateHighlighted];
  [self setBackgroundImage:nil forState:UIControlStateNormal];
  [self setBackgroundImage:[UIImage imageWithColor:LC_COLOR_THIRD_GRAY]
                  forState:UIControlStateHighlighted];
}

- (void)setOrangeStyle {
  [self setLcFont:[LcFont wb17]];
  self.layer.cornerRadius = 5;
  self.layer.masksToBounds = YES;
  [self setBackgroundImage:[UIImage imageWithColor:LC_COLOR_ORANGE] forState:UIControlStateNormal];
  [self setBackgroundImage:[UIImage imageWithColor:LC_COLOR_ORANGE_ACTIVE]
                  forState:UIControlStateHighlighted];
}

- (void)setOrangeStyleWithoutRoundCorner {
  [self setLcFont:[LcFont wb17]];
  [self setBackgroundImage:[UIImage imageWithColor:LC_COLOR_ORANGE] forState:UIControlStateNormal];
  [self setBackgroundImage:[UIImage imageWithColor:LC_COLOR_ORANGE_ACTIVE]
                  forState:UIControlStateHighlighted];
}

- (void)setRedStyle {
  [self setLcFont:[LcFont wb17]];
  self.layer.cornerRadius = 5;
  self.layer.masksToBounds = YES;
  [self setBackgroundImage:[UIImage imageWithColor:LC_COLOR_RED] forState:UIControlStateNormal];
  [self setBackgroundImage:[UIImage imageWithColor:LC_COLOR_RED]
                  forState:UIControlStateHighlighted];
}

- (void)setCommonNavigationStyle {
  const CGFloat HEIGHT = 29;
  const CGFloat HORIZONTAL_PADDING = 8;
  const CGFloat MIN_WIDTH = 55;

  CGRect rect = [self.titleLabel.text boundingRectWithSize:CGSizeMake(MAXFLOAT, HEIGHT)
                                                   options:kNilOptions
                                                attributes:@{
                                                  NSFontAttributeName : self.titleLabel.font
                                                }
                                                   context:nil];
  int width = MAX(rect.size.width + 2 * HORIZONTAL_PADDING, MIN_WIDTH);
  self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, width, HEIGHT);
}

- (void)setCommonBackStyle {
  const CGFloat HEIGHT = 29;
  const CGFloat HORIZONTAL_PADDING = 8;
  CGFloat MIN_WIDTH = 59;
  if ([UIDevice currentDevice].systemVersion.doubleValue < 11.0 && [UIDevice isSmallUISize]) {
    MIN_WIDTH = 44;
  }
  CGRect rect = [self.titleLabel.text boundingRectWithSize:CGSizeMake(MAXFLOAT, HEIGHT)
                                                   options:kNilOptions
                                                attributes:@{
                                                  NSFontAttributeName : self.titleLabel.font
                                                }
                                                   context:nil];
  int width = MAX(rect.size.width + 2 * HORIZONTAL_PADDING, MIN_WIDTH);
  self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, width, HEIGHT);
  if (@available(iOS 11, *)) {
    CGFloat leftMargin = LcUIUtility.navigationBarLeft;
    self.imageEdgeInsets = UIEdgeInsetsMake(0, leftMargin, 0, 0);
  }
}

- (void)enableRoundCorner {
  CGFloat min = MIN(self.bounds.size.width, self.bounds.size.height);
  self.layer.cornerRadius = min * 0.5;
  self.layer.masksToBounds = YES;
}

- (void)setLcFont:(LcFont*)lcFont {
  LC_CHECK_NOT_NULL(lcFont);
  self.titleLabel.font = lcFont.font;
  [self setTitleColor:lcFont.color forState:UIControlStateNormal];
}

- (void)setTitle:(NSString*)title {
  [self setTitle:title forState:UIControlStateNormal];
  [self setTitle:title forState:UIControlStateHighlighted];
  self.titleLabel.text = title;
}

- (void)setAttributedTitle:(NSAttributedString*)title {
  [self setTitle:nil];
  [self setAttributedTitle:title forState:UIControlStateNormal];
  [self setAttributedTitle:title forState:UIControlStateHighlighted];
}

- (void)setImage:(UIImage*)image forAllState:(BOOL)forAllState {
  [self setImage:image forState:UIControlStateNormal];
  if (forAllState) {
    [self setImage:image forState:UIControlStateHighlighted];
  }
}

- (void)addImage:(UIImage*)image rightMargin:(float)rightMargin {
  [self addImage:image rightMargin:rightMargin forAllState:NO];
}

- (void)addImage:(UIImage*)image rightMargin:(float)rightMargin forAllState:(BOOL)forAllState {
  [self setImage:image forAllState:forAllState];

  float margin = rightMargin / 2;
  float imageWidth = image.size.width;
  float labelWidth = [[self titleForState:UIControlStateNormal] sizeWithAttributes:@{
                       NSFontAttributeName : self.titleLabel.font
                     }].width;
  self.imageEdgeInsets = UIEdgeInsetsMake(0, labelWidth + margin, 0, -(labelWidth + margin));
  self.titleEdgeInsets = UIEdgeInsetsMake(0, -(imageWidth + margin), 0, imageWidth + margin);
}

- (void)addImage:(UIImage*)image leftMargin:(float)leftMargin {
  [self addImage:image leftMargin:leftMargin forAllState:NO];
}

- (void)addImage:(UIImage*)image leftMargin:(float)leftMargin forAllState:(BOOL)forAllState {
  [self setImage:image forAllState:forAllState];

  float margin = leftMargin / 2;
  self.imageEdgeInsets = UIEdgeInsetsMake(0, -margin, 0, margin);
  self.titleEdgeInsets = UIEdgeInsetsMake(0, margin, 0, -margin);
}

- (void)setBackgroundImage:(UIImage*)image forAllState:(BOOL)forAllState {
  if (forAllState) {
    [self setBackgroundImage:image forState:UIControlStateNormal];
    [self setBackgroundImage:image forState:UIControlStateHighlighted];
    [self setBackgroundImage:image forState:UIControlStateSelected];
    [self setBackgroundImage:image forState:UIControlStateDisabled];
  } else {
    [self setBackgroundImage:image forState:UIControlStateNormal];
  }
}

#pragma mark -

static char compatibleWithAutoLayoutKey;

+ (UIButton*)commonNavigationButtonCompatibleWithAutoLayoutWithTitle:(NSString*)title
                                                              target:(id)target
                                                              action:(SEL)action {
  UIButton* button = [self commonWhiteNavigationButtonWithTitle:title target:target action:action];
  objc_setAssociatedObject(button, &compatibleWithAutoLayoutKey, @"YES", OBJC_ASSOCIATION_COPY);
  return button;
}

+ (UIButton*)commonWhiteNavigationButtonCompatibleWithAutoLayoutWithTitle:(NSString*)title
                                                                   target:(id)target
                                                                   action:(SEL)action {
  UIButton* button = [self commonWhiteNavigationButtonWithTitle:title target:target action:action];
  objc_setAssociatedObject(button, &compatibleWithAutoLayoutKey, @"YES", OBJC_ASSOCIATION_COPY);
  return button;
}

+ (UIButton*)commonBackButtonCompatibleWithAutoLayoutWithTitle:(NSString*)title
                                                        target:(id)target
                                                        action:(SEL)action {
  UIButton* button = [self commonBlueBackButtonWithTitle:title target:target action:action];
  objc_setAssociatedObject(button, &compatibleWithAutoLayoutKey, @"YES", OBJC_ASSOCIATION_COPY);
  return button;
}

+ (UIButton*)closeButtonCompatibleWithAutoLayoutWithTarget:(id)target action:(SEL)action {
  UIButton* button = [self closeButtonWithTarget:target action:action];
  objc_setAssociatedObject(button, &compatibleWithAutoLayoutKey, @"YES", OBJC_ASSOCIATION_COPY);
  return button;
}

- (CGSize)intrinsicContentSize {
  NSString* isCompatibleWithAutoLayoutString =
      objc_getAssociatedObject(self, &compatibleWithAutoLayoutKey);
  if (isCompatibleWithAutoLayoutString &&
      [isCompatibleWithAutoLayoutString isEqualToString:@"YES"]) {
    return CGSizeMake((self.titleLabel.intrinsicContentSize.width >= 39.0)
                          ? (self.titleLabel.intrinsicContentSize.width + 16.0)
                          : 55.0,
                      29.0);
  }
  return [super intrinsicContentSize];
}

- (void)startLoadingIndicateWithIndicateColor:(UIColor*)color {
  [self endLoadingIndicate];
  self.enabled = NO;
  self.titleLabel.alpha = 0;
  self.imageView.alpha = 0;
  UIActivityIndicatorView* activityIndicatorView = [[UIActivityIndicatorView alloc]
      initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
  activityIndicatorView.color = color;
  [self addSubview:activityIndicatorView];
  activityIndicatorView.center =
      CGPointMake(self.bounds.size.width / 2, self.bounds.size.height / 2);
  [activityIndicatorView startAnimating];
}

- (void)endLoadingIndicate {
  self.enabled = YES;
  self.titleLabel.alpha = 1;
  self.imageView.alpha = 1;
  for (UIView* view in self.subviews) {
    if ([view isKindOfClass:[UIActivityIndicatorView class]]) {
      [view removeFromSuperview];
    }
  }
}

- (void)removeAllTouchUpInsideAction {
  [self removeTarget:nil action:nil forControlEvents:UIControlEventTouchUpInside];
}

@end
